/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.web.action.general;

import com.google.code.kaptcha.Constants;
import foo.bar.blog.core.domain.User;
import foo.bar.blog.core.plugins.UserRegistrationPlugin;
import foo.bar.blog.web.action.BaseActionBean;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.*;
import org.perf4j.aop.Profiled;

/**
 * Actions related to user registration.
 *
 * @author tmjee
 * @version $Date: 2009-03-14 06:53:07 +0000 (Sat, 14 Mar 2009) $ $Id$
 */
public class RegisterActionBean extends BaseActionBean {

    public static final String VIEW = "general/register.jsp";

    private UserRegistrationPlugin userRegistrationPlugin;

    private String captcha;


    @ValidateNestedProperties({
        @Validate(field="username", required = true),
        @Validate(field="password", required = true),
        @Validate(field="email", required = true, converter = EmailTypeConverter.class)
    })
    private User user;

    @Validate(required = true)
    private String confirmPassword;


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }

    public String getCaptcha() {
        return captcha;
    }


    // === View Event ===
    /**
     * Display the user registration page
     * Input - none
     * Output - none
     * @return
     */
    @DontValidate
    @DefaultHandler
    @Profiled(tag="RegisterActionBean_view", logFailuresSeparately = true)
    public Resolution view() {
        return new ForwardResolution(generateThemePath(VIEW));
    }


    /**
     * Perform user registration.
     * Input - user
     * Output - none
     */
    public Resolution register() {
        userRegistrationPlugin.register(user, getContext().getMessages());
        return new RedirectResolution(LoginActionBean.class, "view");
    }
    @ValidationMethod(on = {"register"})
    public void validation_register(ValidationErrors errors) {
        if (user.getPassword() == null || confirmPassword == null || (!user.getPassword().equals(confirmPassword))) {
            errors.add("user.password", new LocalizableError("errors.passwordMismatch"));
        }
        if (captcha != null && !captcha.equals(getSessionAttribute(Constants.KAPTCHA_SESSION_KEY))) {
            errors.add("captcha", new LocalizableError("errors.captchaMismatch"));
        }
        userRegistrationPlugin.validate(user, errors, getContext().getMessages());
    }




    // === Register Event ===
    @SpringBean("userRegistrationPlugin")
    public void setUserRegistrationPlugin(UserRegistrationPlugin userRegistrationPlugin) {
        this.userRegistrationPlugin = userRegistrationPlugin;
    }


}
